package com.lei.day09rikao;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lei.day09rikao.fragment.Fragment1;
import com.lei.day09rikao.fragment.Fragment2;
import com.lei.day09rikao.fragment.Fragment3;
import com.lei.day09rikao.fragment.Fragment4;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tab;
    private ViewPager vp1;
    private List<Fragment> list = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private MyAdapter adapter;
    private ImageView iv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        list.add(fragment4);
        titles.add("同城");
        titles.add("直播");
        titles.add("附近");
        titles.add("来吧");
        adapter = new MyAdapter(getSupportFragmentManager(), list, titles);
        vp1.setAdapter(adapter);
        tab.setupWithViewPager(vp1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("video/*");
                startActivityForResult(intent,101);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101&&resultCode == Activity.RESULT_OK){
            Uri data1 = data.getData();
            iv1.setImageURI(data1);
        }
    }

    private void initView() {
        tab = (TabLayout) findViewById(R.id.tab);
        vp1 = (ViewPager) findViewById(R.id.vp1);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();

        iv1 = (ImageView) findViewById(R.id.iv1);
    }
}
